﻿@model Nop.Plugin.Shipping.FixedByWeightByTotal.Models.ConfigurationModel
@using Nop.Core
@using Nop.Plugin.Shipping.FixedByWeightByTotal.Models
@using Nop.Web.Framework.Models.DataTables
@inject Nop.Core.Domain.Common.AdminAreaSettings adminAreaSettings
@inject Nop.Services.Stores.IStoreService storeService
@inject Nop.Services.Common.IGenericAttributeService genericAttributeService
@inject IWorkContext workContext
@{
    var stores = await storeService.GetAllStoresAsync();
}

@{
    const string hideSearchBlockAttributeName = "FixedByWeightByTotalPage.HideSearchBlock";
    var hideSearchBlock = await genericAttributeService.GetAttributeAsync<bool>(await workContext.GetCurrentCustomerAsync(), hideSearchBlockAttributeName);
}

<script asp-location="Footer">
    $(function() {
        $("#@Html.IdFor(model => model.SearchCountryId)").change(function () {
            var selectedItem = $(this).val();
            var ddlStates = $("#@Html.IdFor(model => model.SearchStateProvinceId)");
            $.ajax({
                cache: false,
                type: "GET",
                url: "@(Url.Action("GetStatesByCountryId", "Country"))",
                data: {
                    "countryId": selectedItem,
                    "addAsterisk": "true"
                },
                success: function (data, textStatus, jqXHR) {
                    ddlStates.html('');
                    $.each(data, function (id, option) {
                        ddlStates.append($('<option></option>').val(option.id).html(option.name));
                    });
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    $("#getStatesByCountryIdAlert").trigger("click");
                }
            });
        });
    });
</script>

<form asp-controller="FixedByWeightByTotal" asp-action="Configure" method="post" id="shipping-byweight-form">
    <div class="cards-group advanced-setting">
        <div class="form-horizontal">
            <div class="card card-default card-search">
                <div class="card-body">
                    <div class="row search-row @(!hideSearchBlock ? "opened" : "")" data-hideAttribute="@hideSearchBlockAttributeName">
                        <div class="search-text">@T("Admin.Common.Search")</div>
                        <div class="icon-search"><i class="fas fa-magnifying-glass" aria-hidden="true"></i></div>
                        <div class="icon-collapse"><i class="far fa-angle-@(!hideSearchBlock ? "up" : "down")" aria-hidden="true"></i></div>
                    </div>

                    <div class="search-body @(hideSearchBlock ? "closed" : "")">
                        <div class="row">
                            <div class="col-md-5">
                                <div class="form-group row">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchStoreId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchStoreId" asp-items="Model.AvailableStores" />
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchWarehouseId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchWarehouseId" asp-items="Model.AvailableWarehouses" />
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchShippingMethodId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchShippingMethodId" asp-items="Model.AvailableShippingMethods" />
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-7">
                                <div class="form-group row">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchCountryId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchCountryId" asp-items="Model.AvailableCountries" />
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchStateProvinceId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchStateProvinceId" asp-items="Model.AvailableStates" />
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchZip" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="SearchZip" />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="text-center col-12">
                                <button type="button" id="search-shipping-byweight-records" class="btn btn-primary btn-search">
                                    <i class="fas fa-magnifying-glass"></i>
                                    @T("Admin.Common.Search")
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card card-default">
                <div class="card-body">
                    <p>
                        <b>@T("Plugins.Shipping.FixedByWeightByTotal.Formula")</b>
                        @T("Plugins.Shipping.FixedByWeightByTotal.Formula.Value")
                    </p>
                    @await Html.PartialAsync("Table", new DataTablesModel
                    {
                        Name = "shipping-byweight-grid",
                        UrlRead = new DataUrl("RateByWeightByTotalList", "FixedByWeightByTotal", null),
                        UrlDelete = new DataUrl("DeleteRateByWeightByTotal", "FixedByWeightByTotal", null),
                        SearchButtonId = "search-shipping-byweight-records",
                        Length = Model.PageSize,
                        LengthMenu = Model.AvailablePageSizes,
                        Filters = new List<FilterParameter>
                        {
                            new FilterParameter(nameof(Model.SearchStoreId)),
                            new FilterParameter(nameof(Model.SearchWarehouseId)),
                            new FilterParameter(nameof(Model.SearchCountryId)),
                            new FilterParameter(nameof(Model.SearchStateProvinceId)),
                            new FilterParameter(nameof(Model.SearchZip)),
                            new FilterParameter(nameof(Model.SearchShippingMethodId))
                        },
                        ColumnCollection = new List<ColumnProperty>
                        {
                            new ColumnProperty(nameof(ShippingByWeightByTotalModel.StoreName))
                            {
                                Title = T("Plugins.Shipping.FixedByWeightByTotal.Fields.Store").Text,
                                Width = "200",
                                Visible = stores.Count > 1
                            },
                            new ColumnProperty(nameof(ShippingByWeightByTotalModel.WarehouseName))
                            {
                                Title = T("Plugins.Shipping.FixedByWeightByTotal.Fields.Warehouse").Text,
                                Width = "200"
                            },
                            new ColumnProperty(nameof(ShippingByWeightByTotalModel.CountryName))
                            {
                                Title = T("Plugins.Shipping.FixedByWeightByTotal.Fields.Country").Text,
                                Width = "200"
                            },
                            new ColumnProperty(nameof(ShippingByWeightByTotalModel.StateProvinceName))
                            {
                                Title = T("Plugins.Shipping.FixedByWeightByTotal.Fields.StateProvince").Text,
                                Width = "200"
                            },
                            new ColumnProperty(nameof(ShippingByWeightByTotalModel.Zip))
                            {
                                Title = T("Plugins.Shipping.FixedByWeightByTotal.Fields.Zip").Text,
                                Width = "200"
                            },
                            new ColumnProperty(nameof(ShippingByWeightByTotalModel.ShippingMethodName))
                            {
                                Title = T("Plugins.Shipping.FixedByWeightByTotal.Fields.ShippingMethod").Text,
                                Width = "200"
                            },
                            new ColumnProperty(nameof(ShippingByWeightByTotalModel.DataHtml))
                            {
                                Title = T("Plugins.Shipping.FixedByWeightByTotal.Fields.DataHtml").Text,
                                Width = "400",
                                Encode = false
                            },

                            new ColumnProperty(nameof(ShippingByWeightByTotalModel.Id))
                            {
                                Title = T("Admin.Common.Edit").Text,
                                ClassName = NopColumnClassDefaults.Button,
                                Width = "100",
                                Render = new RenderCustom("renderColumnEdit")
                            },
                            new ColumnProperty(nameof(ShippingByWeightByTotalModel.Id))
                            {
                                Title = T("Admin.Common.Delete").Text,
                                ClassName = NopColumnClassDefaults.Button,
                                Render = new RenderButtonRemove(T("Admin.Common.Delete").Text),
                                Width = "100"
                            }
                        }
                    })
                </div>
                <div class="card-footer">
                    <button type="submit" id="btnAddNewRecord" class="btn btn-primary"
                            onclick="javascript:OpenWindow('@(Url.Action("AddRateByWeightByTotalPopup", "FixedByWeightByTotal", new {btnId = "btnRefresh", formId = "shipping-byweight-form"}))', 800, 800, true);return false;">
                        @T("Plugins.Shipping.FixedByWeightByTotal.AddRecord")
                    </button>
                    <button type="submit" name="save" id="btnRefresh" class="btn btn-default" style="display: none"></button>
                </div>
            </div>
            <div class="card card-advanced-setting">
                <div class="card-body">
                    <div class="form-group row">
                        <div class="col-md-3">
                            <nop-label asp-for="LimitMethodsToCreated"/>
                        </div>
                        <div class="col-md-9">
                            <nop-editor asp-for="LimitMethodsToCreated"/>
                            <span asp-validation-for="LimitMethodsToCreated"></span>
                        </div>
                    </div>
                    <div class="form-group row">
                        <div class="col-md-3">
                            &nbsp;
                        </div>
                        <div class="col-md-9">
                            <button type="button" id="savegeneralsettings" name="save" class="btn btn-primary">@T("Admin.Common.Save")</button>
                            <script asp-location="Footer">

                                function renderColumnEdit(data, type, row, meta) {
                                    return '<button onclick=\"javascript:OpenWindow(\'@Url.Action("EditRateByWeightByTotalPopup", "FixedByWeightByTotal")?id=' + data + '&btnId=btnRefresh&formId=shipping-byweight-form\', 800, 800, true); return false;\" class="btn btn-default"><i class="fas fa-pencil"></i>@T("Admin.Common.Edit").Text</button>';
                                }

                                $(function() {
                                    $('#savegeneralsettings').click(function() {
                                        var postData = $(this.form).serialize();
                                        addAntiForgeryToken(postData);

                                        $.ajax({
                                            cache: false,
                                            type: "POST",
                                            url: "@Url.Action("Configure", "FixedByWeightByTotal")",
                                            data: postData,
                                            dataType: "json",
                                            success: function (data, textStatus, jqXHR) {
                                                $("#savegeneralsettingsOk").trigger("click");
                                            },
                                            error: function (jqXHR, textStatus, errorThrown) {
                                                $("#savegeneralsettingsError").trigger("click");
                                            }
                                        });
                                        return false;
                                    });
                                });
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script asp-location="Footer">
        $(function () {
            $('#btnRefresh')
                .click(function () {
                    //refresh grid
                    updateTable('#shipping-byweight-grid');

                    //return false to don't reload a page
                    return false;
                });
        });
    </script>

</form>
<nop-alert asp-alert-id="getStatesByCountryIdAlert" asp-alert-message="@T("Admin.Common.Alert.States.Failed")" />
<nop-alert asp-alert-id="savegeneralsettingsOk" asp-alert-message="@T("Admin.Common.Alert.Save.Ok")" />
<nop-alert asp-alert-id="savegeneralsettingsError" asp-alert-message="@T("Admin.Common.Alert.Save.Error")" />